<?php

class SalesController extends BaseController
{
	public $display;

	public function index()
	{
		$products = new ProductService();

		$results = $products->getProducts();

		unset($_SESSION['order_summary']);

		$this->view->setDisplay('categories', $results);

		$this->view->setJavascript(BaliwagConstant::JQUERY_LIB);
		$this->view->setJavascript(BaliwagConstant::JQUERY_UI_JS_LIB);
		$this->view->setJavascript('main');
		$this->view->setJavascript('sales');

		$this->view->setCss('main');
		$this->view->setCss('employee');
		$this->view->setCss('ribbon');
		$this->view->setCss('products');
		$this->view->setCss(BaliwagConstant::JQUERY_UI_CSS_LIB);

		$this->view->setTitle(BaliwagConstant::PAGE_TITLE_WITH_DASH . 'Sales');

		$this->view->template('pages/employee/sales');
	}

	public function orderSumarry()
	{
		if (!empty($_SESSION['order_summary'])) {
			$this->view->setDisplay('products', $_SESSION['order_summary']);
		}

		$this->view->template('components/order_summary');
	}

	public function addOrder()
	{
		$product_id  = $this->post('product_id');
		$product_qty = $this->post('product_qty');

		if (empty($_SESSION['order_summary'])) {
			$_SESSION['order_summary'] = array();
		}

		$exist = $this->checkExistingProduct($product_id);

		$orderSummary = new SalesDetail_Domain();

		$productService = new ProductService();

		$product = $productService->getInfo($product_id);

		$orderSummary->setProduct($product);
		$orderSummary->setProductQty($product_qty);

		$productPrice = $product->getProductPrice();

		$totalPrice = $product_qty * $productPrice;

		$orderSummary->setTotalPrice($totalPrice);

		if (!$exist) {
			$_SESSION['order_summary'][] = $orderSummary;
		} else {
			$this->updateProduct($product_id, $orderSummary, $product, $product_qty);
		}

	}

	public function updateOrder()
	{
		$product_id  = $this->post('product_id');
		$product_qty = $this->post('product_qty');

		$orderSummary = new SalesDetail_Domain();

		$productService = new ProductService();

		$product = $productService->getInfo($product_id);

		$orderSummary->setProduct($product);
		$orderSummary->setProductQty($product_qty);

		$productPrice = $product->getProductPrice();

		$totalPrice = $product_qty * $productPrice;

		$orderSummary->setTotalPrice($totalPrice);

		$this->updateProduct($product_id, $orderSummary, $product, $product_qty, true);
	}

	public function proccessCompleteOrder() {
		$salesSvc = new SalesService();
			
		$result = $salesSvc->saveOrder($_SESSION['order_summary'], $_SESSION['employee']);
			
		if ($result['result']) {
			$data['result'] = true;
				
			$data['product'] = array();
				
			foreach ($_SESSION['order_summary'] as $session) {
				$sessionId = $session->getProduct()->getProductId();

				$product = array();
				$product["product_name"] = $session->getProduct()->getProductName();
				$product["product_qty"] = $session->getProduct()->getProductQty();
				$product["product_price"] = $session->getProduct()->getProductPrice();

				$data['product'][] = $product;
			}
		} else {
			$data['result'] = false;
			$data['message'] = $this->getErrorMessage($result['product']);
		}
			
		echo json_encode($data);
	}

	public function getCurrentProductInfo() {
		$product_id = $this->post('product_id');

		$salesDetail = $this->getProductFromSession($product_id);

		$data['product_id'] = $salesDetail->getProduct()->getProductId();
		$data['product_name'] = $salesDetail->getProduct()->getProductName();
		$data['product_price'] = $salesDetail->getProduct()->getProductPrice();
		$data['product_current_qty'] = $salesDetail->getProduct()->getProductQty();
		$data['current_qty'] = $salesDetail->getProductQty();

		echo json_encode($data);
	}

	public function removeProduct() {
		$product_id = $this->post('product_id');
		$count = 0;

		if (count(($_SESSION['order_summary'] ) > 0)) {
			foreach ($_SESSION['order_summary'] as $session) {
				$sessionId = $session->getProduct()->getProductId();
					
				if ($sessionId == $product_id) {
					$isExisting = true;
					break;
				}
					
				$count++;
			}
				
			unset($_SESSION['order_summary'][$count]);
		} else {
			unset($_SESSION['order_summary']);
		}
	}

	private function checkExistingProduct($product_id)
	{
		$isExisting = false;

		foreach ($_SESSION['order_summary'] as $session) {
			$sessionId = $session->getProduct()->getProductId();

			if ($sessionId == $product_id) {
				$isExisting = true;
				break;
			}
		}

		return $isExisting;
	}

	private function updateProduct($product_id, $orderSummary, $product, $product_qty, $updateAll = false)
	{
		foreach ($_SESSION['order_summary'] as $session) {
			$sessionId = $session->getProduct()->getProductId();

			if ($sessionId == $product_id) {
				$prevTotalPrice = $session->getTotalPrice();
				$prevProductQty = $session->getProductQty();

				$session->setProduct($product);

				$productPrice = $product->getProductPrice();
				$totalPrice   = $product_qty * $productPrice;

				if ($updateAll) {
					$updatedProductQty = $product_qty;
					$updatedTotalPrice = $totalPrice;
				} else {
					$updatedProductQty = $prevProductQty + $product_qty;
					$updatedTotalPrice = $prevTotalPrice + $totalPrice;
				}

				$session->setProductQty($updatedProductQty);
				$session->setTotalPrice($updatedTotalPrice);
			}
		}
	}

	private function getProductFromSession($product_id) {
		$product = null;

		foreach ($_SESSION['order_summary'] as $session) {
			$sessionId = $session->getProduct()->getProductId();

			if ($sessionId == $product_id) {
				$product = $session;

				break;
			}
		}

		return $product;
	}

	private function getErrorMessage($product) {
		$message = 'Product ' . $product->getProductName() . ' is greater than the actual value.';

		return $message;
	}
}